var container = $("#frame");
function ValidateOnly()
{
    var result = "";
	result = "" + SubmitForm('Registrando') + "";
	if(result == "false")
	{
		return false;
	}
}

function postForm(form, path, method)
{
	var result = "";
	result = "" + SubmitForm('Registrando') + "";
	if(result == "false")
	{
		return false;
	}
    
    var is_file = $(form).find("input[type='file']");

    if(is_file.length > 0)
    {   
        return true;
    }else{
        $.ajax({
    		url: "essential/controller/" + path + ".controller.php?function="+method,
    		type: 'post',
    		data: $(form).serialize(),
    		success: function(data){
    			container.html(data);
    		},
    		error: function(){
    		}
    	});
    }
	
	return false;
}

function openForm(path, method, id)
{
	editParameter = "";

	if(id > 0)
	{
		editParameter = "&&id="+id;
	}
	
	$(".loading").remove();
	container.find("#content").html("<div class='loading-page'>Processando</div>");

	$.ajax({
		url: "essential/controller/" + path + ".controller.php?function="+method+""+editParameter,
		success: function(data){
			container.html(data);
		},
		error: function(){
		}
	});
}

function filter(form, path, method)
{
    $(".loading").remove();
	container.find("#list").html("<div class='loading-page'>Processando</div>");
    
	$.ajax({
		url: "essential/controller/" + path + ".controller.php?function="+method,
		type: 'post',
		data: $(form).serialize(),
		success: function(data){
			container.find("#list").html(data);
		},
		error: function(){
		}
	});
	
	return false;
}

function associationList(path, method, id)
{
    $.ajax({
		url: "essential/controller/" + path + ".controller.php?function="+method+"&&id="+id,
		type: 'post',
		success: function(data){
			container.find("#list").html(data);
		},
		error: function(){
		}
	});
	
	return false;
}

function openDetail(path, method)
{
	$(".loading").remove();
	container.find("#content").html("<div class='loading-page'>Processando</div>");

	$.ajax({
		url: "essential/controller/" + path + ".controller.php?function="+method,
		success: function(data){
			container.html(data);
		},
		error: function(){
		}
	});
}


function exclude(path, method, name)
{
	if($("input:checked").length > 0)
	{
		$("input:checked").each(function(e){
			$.ajax({
				url: "essential/controller/" + path + ".controller.php?function="+method+"&&id="+$(this).val(),
				success: function(data){
					container.html(data);
				},
				error: function(){
				}
			});
		});
	}else{
		alert("Selecione um ou mais "+ name +" para exclusão!");
	}
}

function association_action(path, method, name, from_id)
{
     
    
	if($("input:checked").length > 0)
	{
		$("input:checked").each(function(e){
            var validation_message = method == "merge" ? ($(this).hasClass("added") ? "#" + $(this).val() + " ja esta adicionado!" : "") : ($(this).hasClass("added") ? "" : "#" + $(this).val() + " ja esta removido!");           
            
            if(validation_message != "")
            {
                alert(validation_message);
                return false;    
            }
            
                    
			$.ajax({
				url: "essential/controller/" + path + ".controller.php?function="+method+"&&id="+$(this).val()+"&&from_id=" + from_id,
				success: function(data){
					container.find("#list").html(data);
				},
				error: function(){
				}
			});
		});
	}else{
		alert("Selecione um ou mais "+ name +" para esta ação!");
	}
}